<template>
    <div>
        <div style="margin-top: 10px;display: flex;justify-content: center">
            <el-input v-model="keyword" placeholder="通过用户名搜索用户..." prefix-icon="el-icon-search"
                      style="width: 400px;margin-right: 10px" @keydown.enter.native="doSearch"></el-input>
            <el-button icon="el-icon-search" type="primary" @click="doSearch">搜索</el-button>
        </div>
        <div class="user-container">
            <el-card class="user-card" v-for="(user,index) in users" :key="index">
                <div slot="header" class="clearfix">
                    <span>{{user.name}}</span>
                    <el-button style="float: right; padding: 3px 0;color: #e30007;" type="text"
                               icon="el-icon-delete" @click="deleteHr(user)"></el-button>
                </div>
                <div>
                    <div class="img-container">
                        <img v-if="user.userFace == null" src="https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg" :alt="user.name" :title="user.name" class="userface-img">
                        <img v-if="user.userFace != null" :src="user.userFace" :alt="user.name" :title="user.name" class="userface-img">
                    </div>
                    <div class="userinfo-container">
                        <div>用户名：{{user.name}}</div>
                        <div>手机号码：{{user.phone}}</div>
                        <div>电话号码：{{user.telephone}}</div>
                        <div>地址：{{user.address}}</div>
                        <div>用户状态：
                            <el-switch
                                    v-model="user.enabled"
                                    active-text="启用"
                                    @change="enabledChange(user)"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949"
                                    inactive-text="禁用">
                            </el-switch>
                        </div>
                        <div>用户角色：
                            <el-tag type="success" style="margin-right: 4px" v-for="(role,indexj) in user.roles"
                                    :key="indexj">{{role.roleRemark}}
                            </el-tag>
                            <el-popover
                                    placement="right"
                                    title="角色列表"
                                    @show="showPop(user)"
                                    @hide="hidePop(user)"
                                    width="200"
                                    trigger="click">
                                <el-select v-model="selectedRoles" multiple placeholder="请选择">
                                    <el-option
                                            v-for="(r,indexk) in allroles"
                                            :key="indexk"
                                            :label="r.roleRemark"
                                            :value="r.roleId">
                                    </el-option>
                                </el-select>
                                <el-button slot="reference" icon="el-icon-more" type="text"></el-button>
                            </el-popover>
                        </div>
                        <div>备注：{{user.remark}}</div>
                    </div>
                </div>
            </el-card>

        </div>
    </div>
</template>

<script>
    export default {
        name: "SysHr",
        data() {
            return {
                keyword: '',
                users: [],
                selectedRoles: [],
                allroles: []
            }
        },
        mounted() {
            this.initHrs();
        },
        methods: {
            deleteHr(user) {
                this.$confirm('此操作将永久删除【'+user.name+'】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/user/info/"+user.userId).then(resp=>{
                        if (resp) {
                            this.initHrs();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doSearch() {
                this.initHrs();
            },
            hidePop(user) {
                let roles = [];
                Object.assign(roles, user.roles);
                let flag = false;
                if (roles.length != this.selectedRoles.length) {
                    flag = true;
                } else {
                    for (let i = 0; i < roles.length; i++) {
                        let role = roles[i];
                        for (let j = 0; j < this.selectedRoles.length; j++) {
                            let sr = this.selectedRoles[j];
                            if (role.roleId == sr) {
                                roles.splice(i, 1);
                                i--;
                                break;
                            }
                        }
                    }
                    if (roles.length != 0) {
                        flag = true;
                    }
                }
                if (flag) {
					// 更新用户角色
                    let url = '/user/info/?userId=' + user.userId;
                    this.selectedRoles.forEach(sr => {
                        url += '&roleIds=' + sr;
                    });
                    this.putRequest(url).then(resp => {
                        if (resp) {
                            this.initHrs();
                        }
                    });
                }
            },
            showPop(user) {
                this.initAllRoles();
                let roles = user.roles;
                this.selectedRoles = [];
                roles.forEach(r => {
                    this.selectedRoles.push(r.roleId);
                })
            },
            enabledChange(user) {
				console.log(user.enabled);
                delete user.roles;
                this.putRequest("/user/info/enabled", {
					userId: user.userId,
					enabled: user.enabled
				}).then(resp => {
                    if (resp) {
                        this.initHrs();
                    }
                })
            },
            initAllRoles() {
                this.getRequest("/user/info/role").then(resp => {
                    if (resp) {
                        this.allroles = resp;
                    }
                })
            },
            initHrs() {
                this.getRequest("/user/info/",{
					keyword: this.keyword
				}).then(resp => {
                    if (resp) {
                        this.users = resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .userinfo-container div {
        font-size: 12px;
        color: #409eff;
    }

    .userinfo-container {
        margin-top: 20px;
    }

    .img-container {
        width: 100%;
        display: flex;
        justify-content: center;
    }

    .userface-img {
        width: 72px;
        height: 72px;
        border-radius: 72px;
    }

    .user-container {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
    }

    .user-card {
        width: 350px;
        margin-bottom: 20px;
    }
</style>